/*
 * main.cpp
 *
 *  Created on: Jul 11, 2010
 *      Author: satish
 */
#include<iostream>
#include<string>
#include<string.h>
#include<stdlib.h>
#include<fstream>
#include"googlesearch.h"
#include"kmpsearchalgorithm.h"
#include"naivesearchalgorithm.h"
#include"bitapsearchalgorithm.h"

using namespace std;


int main()
{
     //Create an instance of type NaiveSearchAlgorithm
	NaiveSearchAlgorithm naivesearch;
	//Create an instance of type KmpSearchAlgorithm
	KmpSearchAlgorithm kmpsearch;
	//Create an object of type NaiveSearchAlgorithm
	BitapSearchAlgorithm bitapsearch;
	//create an object of type GoogleSearch by passing a reference to NaiveSearchAlgorithm object

	GoogleSearch firstsearch(&naivesearch);
	GoogleSearch secondsearch(&kmpsearch);
	GoogleSearch thirdsearch(&bitapsearch);

	string sourcefile;//a string that contains the source file name 
	char* pos = NULL;
	int noOfOccurences = 0;
	string haystack,needle,line;


	cout<<"Enter the source file:"<<endl;
	getline(cin,sourcefile);
    ifstream infile(sourcefile.c_str());

	cout<<"Enter the substring you want to search for: "<<endl;
	getline(cin,needle);
	//read the entire inputfile into the string
	while(getline(infile,line))
	{
		haystack.append(line);
		haystack.append("\n");
	}
	//Naive search
	if(( pos =	firstsearch.substringSearch( haystack.c_str(), needle.c_str()))!= NULL)
	{
		noOfOccurences++;
		while((pos = firstsearch.substringSearch(++pos,needle.c_str()))!=NULL)
		{
			noOfOccurences++;
		}
		cout<<"Naive Search : The needle was found in the haystack "<<noOfOccurences<<" time(s)"<<endl;
		noOfOccurences = 0;
	}
    else
    cout<<"Naive Search : The needle was not found in the haystack" <<pos;

	//KMP Search
	if(( pos =	secondsearch.substringSearch( haystack.c_str(), needle.c_str()))!= NULL)
	{
		noOfOccurences++;
		while((pos = secondsearch.substringSearch(++pos,needle.c_str()))!=NULL)
		{
			noOfOccurences++;
		}
		cout<<"KMP Search : The needle was found in the haystack "<<noOfOccurences<<" time(s)"<<endl;
		noOfOccurences = 0;
	}
	else
	cout<<"KMP Search : The needle was not found in the haystack" <<pos;

	//Bitap Search
	if(( pos =	thirdsearch.substringSearch( haystack.c_str(), needle.c_str()))!= NULL)
	{
		noOfOccurences++;
		while((pos = thirdsearch.substringSearch(++pos,needle.c_str()))!=NULL)
		{
			noOfOccurences++;
		}
		cout<<"Bitap Search : The needle was found in the haystack "<<noOfOccurences<<" time(s)"<<endl;
	}
	else
	    cout<<"Bitap Search : The needle was not found in the haystack" <<pos;

	return 0;
}
